package com.ice_col.clock.myview;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ice_col.clock.R;

import java.util.Calendar;

/**
 * Created by Administrator on 2015/8/24 0024.
 */
public class ViewClock extends LinearLayout {

    private TextView tv_time;


    private Handler updateTimeHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            reflashTime();
        }
    };

    @Override
    protected void onVisibilityChanged(View changedView, int visibility) {
        if(visibility==View.VISIBLE){
            updateTimeHandler.sendEmptyMessage(0);
        }else{

        }
    }

    private void reflashTime(){
        Calendar calendar=Calendar.getInstance();
        tv_time.setText(String.format("%d:%d:%d",calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),calendar.get(Calendar.SECOND)));
        if(getVisibility()== View.VISIBLE){
            updateTimeHandler.sendEmptyMessageDelayed(0,1000);
        }
    }
    public ViewClock(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ViewClock(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ViewClock(Context context) {
        super(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tv_time = (TextView) findViewById(R.id.tabTime_time);
       updateTimeHandler.sendEmptyMessage(0);
    }
}
